#include "AddPatientUi.h"
#include "ui_AddPatientUi.h"
#include "WidgetHandler.h"
#include "BootSetting.h"
#include "MedicalInfo.h"
#include "Database.h"
#include <QDateTime>
#include "HintDialog.h"
#include "MiscData.h"
#include "Medio.h"
#include "LogoUtility.h"
#include "Mcu.h"

AddPatientUi::AddPatientUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::AddPatientUi)
{
    ui->setupUi(this);
    LogoUtility::getInstance()->addLogoLabel(ui->logo);
    QRegExp rx("[0-9]+$");
    QRegExpValidator *validator = new QRegExpValidator(rx, this);
    ui->id->setValidator(validator);
    ui->age->setValidator(validator);
}

AddPatientUi::~AddPatientUi()
{
    delete ui;
}

void AddPatientUi::activate()
{
    QList<QLineEdit*> list = ui->widget->findChildren<QLineEdit*>();
    for(auto &item:list){
        item->clear();
    }
}

void AddPatientUi::inactivate()
{

}

void AddPatientUi::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}
void AddPatientUi::on_gohome_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void AddPatientUi::on_goback_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void AddPatientUi::on_add_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    if (checkInfo() < 0) {
        HintDialog::getInstance()->setInfo(tr("必填信息不能为空!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }

    if (addOnePatientInfo() < 0) {
        HintDialog::getInstance()->setInfo(tr("新建患者失败, 此ID已存在!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }

    HintDialog::getInstance()->setInfo(tr("新建患者成功!"), HintDialogType_Hint);
    HintDialog::getInstance()->exec();
}

void AddPatientUi::on_applay_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    if (checkInfo() < 0) {
        HintDialog::getInstance()->setInfo(tr("必填信息不能为空!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }

    if (addOnePatientInfo() << 0) {
        HintDialog::getInstance()->setInfo(tr("新建患者失败，无法应用!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return;
    }

    RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentApplyPatientNumber = g_freeNumber;
    RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentSelectPatientNumber = g_freeNumber;

    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

int AddPatientUi::addOnePatientInfo()
{
    PatientInfo info;

    g_freeNumber = Database::getInstance()->getFreePatientNumber();
    qDebug()<<"free number = "<<g_freeNumber;

    info.number = g_freeNumber;
    info.id = ui->id->text();
    info.name = ui->name->text();
    info.age = ui->age->text().toInt();
    info.gender = ui->gender->currentIndex();
    info.visitTime = QString("%1").arg(QDateTime::currentDateTime().toString("yyyy/MM/dd"));


    return Database::getInstance()->addPatient(&info);
}

int AddPatientUi::checkInfo()
{
    if(ui->id->text().isEmpty()||
        ui->name->text().isEmpty()) {

        return -1;
    }
    return 1;
}
